package com.ihr360.cnb.module.insurance.controller;

import com.alibaba.fastjson.JSONObject;
import com.ihr360.cnb.module.insurance.service.StaffBenefitImportService;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.excel.vo.ImportRequest;
import com.ihr360.insurance.entity.request.StaffBenefitRequest;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import com.ihr360.type.StaffStatus;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author Stone.Shi
 * @description 员工社保公积金档案导入、导出
 * @date 2018-05-30 10:50:40.
 */
@RestController
@RequestMapping("/staffBenefits")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STAFF_SIHF_ARCHIVE_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STAFF_SIHF_ARCHIVE_NAME)
public class StaffBenefitImportController {

    private static final String TEMPLATE_FILE_NAME = "社保公积金档案导入模板.xls";

    @Autowired
    private StaffBenefitImportService importService;

    @FunctionDefinition(functionName = "社保公积金员工档案导入模板下载", functionCode = ModuleCodeStore.Function.IMPORT)
    @RequestMapping(value = "/excel/template", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void downloadExcelTemplate(@RequestParam String companyId,
                                      HttpServletResponse response) throws IOException {
        response.setHeader("x-filename", URLEncoder.encode(TEMPLATE_FILE_NAME, "UTF-8"));
        importService.downloadTemplateExcel(companyId, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "社保公积金员工档案导入", functionCode = ModuleCodeStore.Function.IMPORT)
    @RequestMapping(value = "/excel/import", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultInfo importExcel(@RequestParam String companyId,
                                  @RequestParam String userId,
                                  @RequestBody ImportRequest importRequest) {
        importService.importExcel(companyId, userId, importRequest);
        return new ResultInfo.Builder<>().success();
    }

    @FunctionDefinition(functionName = "社保公积金在职员工档案导出", functionCode = ModuleCodeStore.Function.EXPORT)
    @RequestMapping(value = "/excel/export/inService", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportExcelInService(@RequestParam String companyId,
                                     @RequestParam String userId,
                                     @RequestParam String reqString,
                                     HttpServletResponse response) throws IOException {
        StaffBenefitRequest request = new StaffBenefitRequest();
        try {
            request = JSONObject.parseObject(reqString, StaffBenefitRequest.class);
        } catch (Exception e) {
            ValidationUtils.reject("", "下载参数错误");
        }
        request.setStaffStatus(StaffStatus.IN_SERVICE);
        response.setHeader("x-filename", URLEncoder.encode("在职员工社保公积金档案.xls", "UTF-8"));
        importService.exportExcel(companyId, userId, request, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "社保公积金离职员工档案导出", functionCode = ModuleCodeStore.Function.EXPORT)
    @RequestMapping(value = "/excel/export/quit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportExcelQuit(@RequestParam String companyId,
                                @RequestParam String userId,
                                @RequestParam String reqString,
                                HttpServletResponse response) throws IOException {
        StaffBenefitRequest request = new StaffBenefitRequest();
        try {
            request = JSONObject.parseObject(reqString, StaffBenefitRequest.class);
        } catch (Exception e) {
            ValidationUtils.reject("", "下载参数错误");
        }
        request.setStaffStatus(StaffStatus.QUIT);
        response.setHeader("x-filename", URLEncoder.encode("离职员工社保公积金档案.xls", "UTF-8"));
        importService.exportExcel(companyId, userId, request, response.getOutputStream());
    }
}
